<?php

defined('_SECURITY_TOKEN') or die("caia fora");

class modelmetadata extends model
{
	
	function __construct(  )
	{
		
		
		
	}
	

	
	public function getItem( $user_id, $id ){
				
		$result=false;
		
		try{
				 		
			$db = application::getDb( configuration::db_type);
	// 		
			$db->setParameters(configuration::db_server, 
								configuration::db_port, 
								configuration::db_username, 
								configuration::db_password,
								configuration::db_name);
	
			if( !empty($id)){
						
				$query = sprintf("SELECT id,metadata FROM \"%s%s\" 
								WHERE user_id=%u and id=%u;",
						configuration::table_prefix,
	                 	configuration::table_view, 
	                 	application::getUser_id(), 
	                 	$id);
						
				$result = $db->getQuery($query);
			}
		
		}
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
		
		//$query = sprintf("SELECT  * FROM %s WHERE user_id=%d;",
        //         	configuration::table_view, application::getUser_id());

		

		return $result;
		
	}
	
	
	
	public function save( $parameters ){
				
		$result=false;
				
		try {
        
			$db = application::getDb( configuration::db_type);
	// 		
			$db->setParameters(configuration::db_server, 
								configuration::db_port, 
								configuration::db_username, 
								configuration::db_password,
								configuration::db_name);

			$query = sprintf("UPDATE \"%s%s\"  SET metadata='%s'
							WHERE id=%u and user_id=%u",
							configuration::table_prefix,
		                 	configuration::table_view, 
		                 	urldecode($parameters->metadata),
							$parameters->id,
							application::getUser_id());
					
			try{
				$result = $db->setQuery($query);
			}
			catch (AppException $e) {
		    	$result=false;
		    }
			
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
	
		return $result;
		
	}
	
}